(function(){
    angular.module('app').controller("dspProfitCtrl",function($scope,proxy){
        $scope.permission = PERMISSION;
        $scope.finance = dspFinance;
        $scope.profitsObj = {
            id:"",
            value: "",
            type: ""
        }
        $scope.filter = {}
        // 显示利润提取弹框
        $scope.showProfits = function(item,type) {
            $scope.profitsObj = {
                id: item.agencyID,
                value: "",
                type: type
            }
            if(type == 'rtb') {
                $scope.popConfig = {
                    "title": "利润提取",
                    "placeholder": "请输入提取利润金额(元)"
                }
                $scope.profitsObj.money = item.unExtractRtbProfit;
            } else {
                $scope.popConfig = {
                    "title": "收入提取",
                    "placeholder": "请输入提取收入金额(元)"
                }
                $scope.profitsObj.money = item.unExtractPdbProfit;
            }
            layer.open({
                type: 1,
                title: $scope.popConfig.title,
                content: $('.profits'),
                area:["422px","250px"]
            })
        }
        $scope.search = function(){
            getList();
        };

        $scope.profits = function() {
            var profitsObj = $scope.profitsObj;
            var data = {
                "agencyID": profitsObj.id,//广告公司ID
                "action":profitsObj.type,//操作转出 rtbProfit,pdbProfit
                "money": profitsObj.value,//金额
                "comment": profitsObj.comment//金额
            }
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/saas/dsp/finance/profit',
                load: true,
                data: {
                    data: data
                },
                success: function(){
                    layer.closeAll();
                    layer.msg("提取成功");
                    getList();
                    getFinance();
                }
            })
        }

        // 下载报表
        $scope.download = function() {
            var data = {
                'page': "1",
                'size': "99999",
                "sort": ["cTime,1"],
                'filter': $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/saas/dsp/finace/agency',
                load: true,
                data: data,
                success: function(res){
                    exportCsv({
                        title: ['广告主','总竞价交易利润','竞价交易未提取利润','竞价交易已提取利润','总保价保量收入','保价保量未提取收入','保价保量已提取收入'],
                        titleForKey: ['name','rtbProfit','unExtractRtbProfit','extractRtbProfit','pdbProfit','unExtractPdbProfit','extractPdbProfit'],
                        data: res.data.data,
                    },"利润提取.csv");
                }
            })
        }

        getList();
        getFinance();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                "sort": ["cTime,1"],
                'filter': $scope.filter
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/saas/dsp/finace/agency',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }
        function getFinance() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/saas/dsp/finace/summary',
                data: {},
                success: function(res){
                    $scope.finance = res.data;
                }
            })
        }
    })
})();